package com.beself.service.impl;

import com.beself.dao.*;
import com.beself.entity.*;
import com.beself.service.HabitService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
public class HabitServiceImpl implements HabitService {

    @Resource
    private HabitMapper habitMapper;
    @Resource
    private UserHabitsMapper userHabitsMapper;
    @Resource
    private TasksMapper tasksMapper;
    @Resource
    private DatetimeMapper dtMapper;
    @Resource
    private DateStatusMapper dStatusMapper;
    @Resource
    private DtasksMapper dtasksMapper;



//    首页展示习惯
    @Override
    public List<Habits> show(Integer uId) {
        System.out.println("uId = " + uId);
//        通过用户id找用户习惯
        List<userHabits> hids = userHabitsMapper.findhid(uId);
        System.out.println("hids = " + hids);

        List list = new ArrayList();
        for (userHabits hid : hids) {
            System.out.println("hid = " + hid);
            System.out.println("hid.gethId() = " + hid.gethId());

//            Map map = new HashMap();
//            map.put("uid",hid.getuId());
//            map.put("hid",hid.gethId());
            Habits habit = habitMapper.findHabits(hid.gethId());
            System.out.println("habit = " + habit);
            list.add(habit);
        }
        return list;
    }

    //    通过用户id和习惯id找任务
    @Override
    public List<Tasks> showTask(Integer uId,Integer hId ) {

        Map m = new HashMap();

        m.put("uid",uId);

        m.put("hid",hId);

        List<Tasks> all = tasksMapper.findAll(m);
        System.out.println("all = " + all);

        return all;
    }

    //    修改任务时间长度

    @Override
    @Transactional
    public Integer updateTimespan(Integer tId, Integer tTimespan) {

        Map map = new HashMap();

        map.put("tId",tId);
        map.put("tTimespan",tTimespan);
        Integer integer = tasksMapper.updateTime(map);


        return integer;
    }

//    删除任务
    @Override
    @Transactional
    public Integer delTask(Integer tId) {

        Integer integer = tasksMapper.delTask(tId);

        return integer;
    }


//    添加任务

    @Override
    @Transactional
    public Integer addTask(Integer uId,Integer hId, Tasks tasks) {
        Map map = new HashMap();
        map.put("uid",uId);
        map.put("hid",hId);
        map.put("tname",tasks.gettName());
        map.put("tstatus",tasks.gettStatus());
        map.put("tdate",tasks.gettDate());
        map.put("tstatuscode",tasks.gettStatuscode());
        map.put("ttimespan",tasks.gettTimespan());
        map.put("tuptime",tasks.gettUptime());
        map.put("uid",tasks.getuId());
        map.put("detail",tasks.getDetail());
        map.put("data2",tasks.getData2());

        Integer integer = tasksMapper.addTask(map);


        return integer;
    }

//    添加任务2，默认列表的
    @Override
    public Integer addTask2(String tName) {
        return dtasksMapper.addDtask(tName);
    }

    //    新增习惯
    @Override
    @Transactional
    public Integer addHabit(Integer uId, Habits habit) {

        System.out.println("uId = " + uId);
        System.out.println("habit = " + habit);

        Integer integer = habitMapper.addHabit(habit);
        System.out.println("aaaa"+habit.gethId());

        System.out.println("bbb="+uId);
        Map map = new HashMap();
        map.put("uid",uId);
        map.put("hid",habit.gethId());
        Integer add = userHabitsMapper.add(map);
        System.out.println("uId = " + uId);

        System.out.println("add = " + add);
        if(integer>0 && add >0){
            System.out.println("添加成功");
            return 1;
        }else {
            System.out.println("添加失败");
            return 0;
        }
    }

//    删除习惯
    @Override
    @Transactional
    public Integer deleteHabit(Integer uId, Integer hId) {

        Map map = new HashMap();
        map.put("uid",uId);
        map.put("hid",hId);

        Integer dele = userHabitsMapper.dele(map);

        Integer integer = tasksMapper.delTaskOne(map);
        Integer integer1 = habitMapper.delHabit(map);
        if (dele<1 && integer<1 && integer1<1 ){
            return 0;
        }

        return 1;
    }

    @Override
    public List<DTasks> queryAll() {

        List<DTasks> dtTasks = dtasksMapper.findAll();

        return dtTasks;
    }



    @Override
    public DTasks queryOne(Integer tId) {

        DTasks byId = dtasksMapper.findById(tId);
        return byId;
    }


//    日历查询
    @Override
    public DateStatus queryStatus(Integer uId, Date tDatetime) {
        Map map = new HashMap();

        map.put("uid",uId);
        map.put("tdatetime",tDatetime);

        return dStatusMapper.findDT(map);
    }

//    补签卡
    @Override
    @Transactional
    public Integer upDateRe(Integer uId, Date tDatetime) {
        Map map = new HashMap();

        map.put("uid",uId);
        map.put("tdatetime",tDatetime);

        return dStatusMapper.updateHSstatus(map);
    }

    @Override
    @Transactional
    public Integer updateTaskstatus(Integer tId) {

        Integer integer = tasksMapper.updateTaskStatus(tId);

        return integer;
    }

    @Override
    public List<Tasks> findTasks(Integer uId) {
        return tasksMapper.findByuId(uId);
    }

//    新用户绑定默认习惯
    @Override
    @Transactional
    public Integer addUserHabits(Integer uId) {
        Integer integer = userHabitsMapper.addUserHabit(uId);

        return integer;
    }


}